package com.nk.iexam.adapter;

import java.util.List;

import com.nk.iexam.R;
import com.nk.iexam.models.Category;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CategoryAdapter extends ArrayAdapter<Category>
{
	private Context _context;
	private int _layoutResourceId;  
	private List<Category> _data = null;

	public CategoryAdapter(Context context_, int layoutResourceId_, List<Category> data_) 
	{
		super(context_, layoutResourceId_, data_);
		this._layoutResourceId = layoutResourceId_;
		this._context = context_;
		this._data = data_;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		CategoryHolder holder = null;

		if(row == null)
		{
			LayoutInflater inflater = ((Activity)_context).getLayoutInflater();
			row = inflater.inflate(_layoutResourceId, parent, false);

			holder = new CategoryHolder();
			holder.imgIcon = (ImageView)row.findViewById(R.id.list_image);
			holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);

			row.setTag(holder);
		}
		else
		{
			holder = (CategoryHolder)row.getTag();
		}

		Category category = _data.get(position);
		holder.txtTitle.setText(category.getName());
		holder.imgIcon.setImageResource(R.drawable.folder);
		
		return row;
	}

	static class CategoryHolder
	{
		ImageView imgIcon;
		TextView txtTitle;
	}
}
